import operate from '@dfeidao/atom-nodejs/db/operate';
import query from '@dfeidao/atom-nodejs/db/query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { v4 as uuid } from 'uuid';

interface Message {
	productid: string;
	level_no: string;
	zjd: [{ _id: string }];
	fjd: [{ _id: string }];
	client_type: string;
}
interface D {
	formid: string;
}
export default async function atom(msg: Message, action_id: string, session_id: string) {
	log('Service begin');
	// todo
	const { del, insert, exec } = operate(action_id, session_id, '12');
	del('tenant_level_form', { productid: msg.productid, level_no: msg.level_no, client_type: msg.client_type });
	const { prepare, exec: a } = query(action_id, session_id, '12');
	prepare('tenant_level_form', ['formid'], { productid: msg.productid, level_no: msg.level_no, client_type: msg.client_type }, 200, 1, [], []);
	const [data] = await a<D>();
	const fidarr = new Array();
	data.forEach((v) => {
		fidarr.push(v.formid);
	});
	del('tenant_level_form_code', { productid: msg.productid, level_no: msg.level_no, formid: { $in: fidarr } });
	msg.fjd.forEach((i) => {
		i._id = uuid();
		insert('tenant_level_form', i);
	});
	msg.zjd.forEach((i) => {
		i._id = uuid();
		insert('tenant_level_form_code', i);
	});
	const ret = await exec();
	log('Service end');
	if (ret.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
